package com.war.util;


        import com.war.pojo.system.User;
        import org.springframework.stereotype.Component;

        import javax.servlet.*;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpSession;
        import java.io.IOException;

/**
 * 跨域访问过滤器
 */
@Component
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //转换成request对象
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        //获取session对象
        HttpSession session = request.getSession();
        //获取登录用户
        User user = (User)session.getAttribute("user");
        //获取当前的url
        String url = request.getRequestURI()+"";


        //放行静态资源
//        if (url.endsWith("js")||url.endsWith("css")||
//                url.endsWith("jpg")||url.endsWith("png")||url.endsWith("gif")||
//                url.endsWith("svg")||url.endsWith("eot")||url.endsWith("ttf")||url.endsWith("woff") ){
//            //不拦截，放行
//            filterChain.doFilter(servletRequest,servletResponse);
//            return;
//        }
        switch (url){
            //入库管理 只能采购员访问
            case("/warehousing/purchaseList"):
            case("/warehousing/purchaseDetail"):
            case("/warehousing/purchaseEdit"):
            case("/warehousing/insertPurchase"):
            case("/warehousing/purchaseReturn"):
            case("/return"):
            case("/insertReturn"):
            case("/returnDetail"):
            case("/returnEdit"):
                if (user == null){
                    //未登录状态，回到登录页面
                    request.getRequestDispatcher("/login/loginPage").forward(servletRequest,servletResponse);
                    return;
                }
                switch (user.getIdentity()){
                    case("sys_admin"):
                    case("war_admin"):
                        //显示权限提示页面
                        request.getRequestDispatcher("userAdmin/alert").forward(servletRequest,servletResponse);
                        return;
                    default:
                        //不拦截，放行
                        filterChain.doFilter(servletRequest,servletResponse);
                        break;
                }
                break;
            //出库管理、库存管理 只能仓库管理员访问
            case("/outOfStorage/out"):
            case("/outOfStorage/add"):
            case("/outOfStorage/getDetail"):
            case("/outOfStorage/update"):
            case("/outOfStorage/productReturn"):
            case("/productReturn/return"):
            case("/productReturn/addReturn"):
            case("/productReturn/getDetail"):
            case("/productReturn/update"):
            case("/damages"):
            case("/damages/add"):
            case("/damages/edit"):
            case("/transfer"):
            case("/transfer/add"):
            case("/transfer/edit"):
                if (user == null){
                    //未登录状态，回到登录页面
                    request.getRequestDispatcher("/login/loginPage").forward(servletRequest,servletResponse);
                    return;
                }
                switch (user.getIdentity()){
                    case("sys_admin"):
                    case("buyer"):
                        //显示权限提示页面
                        request.getRequestDispatcher("userAdmin/alert").forward(servletRequest,servletResponse);
                        return;
                    default:
                        //不拦截，放行
                        filterChain.doFilter(servletRequest,servletResponse);
                        break;
                }
                break;
            //基础数据管理、安全管理 只能系统管理员访问
            case("/buyer/buyerList"):
            case("/buyer/addBuyPage"):
            case("/buyer/editBuyPage"):
            case("/customer/customerList"):
            case("/customer/addCusPage"):
            case("/customer/editCusPage"):
            case("/supplier/supplierList"):
            case("/supplier/addSupPage"):
            case("/supplier/editSupPage"):
            case("/warehouse/warehouseList"):
            case("/warehouse/addWarePage"):
            case("/warehouse/editIWarePage"):
            case("/userAdmin/list"):
            case("/userAdmin/addUserPage"):
            case("/userAdmin/editIdentityPage"):
                if (user == null){
                    //未登录状态，回到登录页面
                    request.getRequestDispatcher("/login/loginPage").forward(servletRequest,servletResponse);
                    return;
                }
                switch (user.getIdentity()){
                    case("buyer"):
                    case("war_admin"):
                        //显示权限提示页面
                        request.getRequestDispatcher("userAdmin/alert").forward(servletRequest,servletResponse);
                        return;
                    default:
                        //不拦截，放行
                        filterChain.doFilter(servletRequest,servletResponse);
                        break;
                }
                break;
            //统计报表、主页 都能访问
            case("/chart"):
            case("/index/welcome"):
            case("/index/homePage"):
            case("/index/updateUserPage"):
                if (user == null){
                    //未登录状态，回到登录页面
                    request.getRequestDispatcher("/login/loginPage").forward(servletRequest,servletResponse);
                    return;
                }
                //不拦截，放行
                filterChain.doFilter(servletRequest,servletResponse);
                return;
            default:
                //不拦截，放行
                filterChain.doFilter(servletRequest,servletResponse);
                break;
        }
    }

    @Override
    public void destroy() {

    }
}
